from apps.models import BaseModel

from ext import db


# 类目库
class Bookcategory(db.Model):
    __tablename__ = 'book_categories'

    id = db.Column(db.Integer,
                   primary_key=True,
                   nullable=False,
                   autoincrement=True)
    name = db.Column(db.String(200), nullable=False)

    # relationship
    books = db.relationship('Book', backref='book_category', lazy='dynamic')


# 标签库
class BookTag(db.Model):
    __tablename__ = 'book_tags'

    id = db.Column(db.Integer,
                   primary_key=True,
                   nullable=False,
                   autoincrement=True)
    name = db.Column(db.String(200), nullable=False)

    # relationship
    books = db.relationship('Book',
                            secondary='book_booktag',
                            back_populates='book_tags',
                            lazy='dynamic')


# 作者
class Author(db.Model):
    __tablename__ = 'authors'

    # ID
    id = db.Column(db.Integer,
                   primary_key=True,
                   nullable=False,
                   autoincrement=True)
    # 名字
    name = db.Column(db.String(200), nullable=False)
    # 简介
    intro = db.Column(db.Text())
    # 头像
    portrait = db.Column(db.String(200))
    # 关注量
    follow_num = db.Column(db.Integer, nullable=False, default=0)
    # 是否删除
    is_del = db.Column(db.Boolean(), default=False)
    del_time = db.Column(db.DateTime())

    # relationship
    books = db.relationship('Book', backref='author', lazy='dynamic')


# 书
class Book(BaseModel):
    __tablename__ = 'books'

    # 标题
    title = db.Column(db.String(200), nullable=False)
    # 简介
    intro = db.Column(db.Text())
    # 详细描述
    description = db.Column(db.Text())
    # 评分
    score = db.Column(db.Float, nullable=False)
    # 评分人数
    score_num = db.Column(db.Integer, nullable=False)
    # 封面
    cover = db.Column(db.String(200))
    # 加入书架量
    bookshelf_num = db.Column(db.Integer, nullable=False, default=0)

    # 关键词,以','分割
    keywords = db.Column(db.String(200))

    # 是否删除
    is_del = db.Column(db.Boolean(), default=False)
    del_time = db.Column(db.DateTime())

    # 是否下架
    is_off = db.Column(db.Boolean(), default=False)
    off_time = db.Column(db.DateTime())

    # 外键
    # 作者ID
    author_id = db.Column(db.Integer,
                          db.ForeignKey('authors.id'),
                          nullable=True)
    # 书类目ID
    book_category_id = db.Column(db.Integer,
                                 db.ForeignKey('book_categories.id'),
                                 nullable=True)

    # relationship
    # 标签
    book_tags = db.relationship('BookTag',
                                secondary='book_booktag',
                                back_populates='books',
                                lazy='dynamic')


# 书的标签
class BookBooktag(BaseModel):
    __tablename__ = 'book_booktag'

    # 书ID
    book_id = db.Column(db.Integer, db.ForeignKey('books.id'), nullable=False)
    # 标签ID
    book_tag_id = db.Column(db.Integer,
                            db.ForeignKey('book_tags.id'),
                            nullable=False)

    # 是否删除
    is_del = db.Column(db.Boolean(), default=False)


# 章节
class Chapter(db.Model):
    __tablename__ = 'chapters'
    # 章节ID
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    # 书ID
    book_id = db.Column(db.Integer, nullable=True)
    # 标题
    title = db.Column(db.String(200), nullable=False)
    # 内容
    content = db.Column(db.Text())
    # 阅读量
    read_count = db.Column(db.Integer, nullable=False, default=0)

    # 是否删除
    is_del = db.Column(db.Boolean(), default=False)
    del_time = db.Column(db.DateTime())


# 书评
class BookReview(BaseModel):
    __tablename__ = 'book_reviews'

    # 用户ID
    user_id = db.Column(db.Integer, nullable=True)
    # 书ID
    book_id = db.Column(db.Integer, nullable=True)

    # 书评内容
    content = db.Column(db.Text())
    # 评分
    score = db.Column(db.Float, nullable=False)

    # 是否删除
    is_del = db.Column(db.Boolean(), default=False)
    del_time = db.Column(db.DateTime())
